import config from '@/common/config'
const install = (Vue, vm) => {
	let u = {};
	u.getOptions = function(options) {
			let sr = [];
			let scene = options.scene;
			if (scene != null) {
				console.log(scene);
				let pp = decodeURIComponent(scene);
				let pps = pp.split("&");
				for (let i = 0, j = pps.length; i < j; i++) {
					let iitem = pps[i].split("=");
					sr[iitem[0]] = iitem[1];
				}
			}
			for (let index in options) {
				sr[index] = options[index];
			}
			return sr;
		},
		u.test = function() {
			console.log('testtest');
		}		
	u.initLocale = function(options) {
		let that = vm;
		if (that.$i18n.locale == null || that.$i18n.locale == '') {
			let locales = (that.options.locales != null && that.options.locales != '') ? that.options.locales :
				'zh';
			that.$i18n.locale = locales;
		}
	}
	u.getBaseurl = function() {
			let baseurl = uni.getStorageSync("csmbaseurl");
			if (baseurl == null || baseurl == '') {
				baseurl = config.baseUrl;
			}
			// H5模式下如果为传递地址,则默认为当前域名计算api地址
			//#ifdef H5
			if (window && baseurl == null || baseurl == '') {
				baseurl = window.location.protocol + "//" + window.location.host + "/api/";
			}
			//#endif
			return baseurl;
		},
		/**
		 * Ajax Get请求
		 * 
		 * @param {Object} url
		 * 		基于api的相对地址,如: csmconf/conf/confs
		 * @param {Object} param
		 * 		get的数组参数,如:{id:1}
		 * @param {Object} success
		 * 		执行成功的方法 , function(res){}
		 */
		u.ajaxget = function(url, param, success) {
			let that = this;
			// let baseurl = u.getBaseurl();
			// let http_url = baseurl + url;

			// //clogin由于使用的fa_user的登录因为后台语言更新,会造成后台预览英文版后整个网站都变成英文版,考虑到其他页面不会使用其后台的语言,所以都强制Wie中文版
			// if( !(http_url.indexOf('?lang=')>=0 || http_url.indexOf('?lang=')>=0) ){
			// 	let sep = "?";
			// 	if(http_url.indexOf('?')>=0){
			// 		sep = "&";
			// 	}
			// 	http_url += sep + "lang=zh-cn";
			// }
			let http_url = that._adjust_ajaxurl(url);

			console.log('invoke ajaxget:' + http_url, param);
			uni.$u.http.get(http_url, {
				params: param
			}).then(res => {
				success(res);
			}).catch(err => {

			})
		};
	/**
	 * 用于ajax get和ajax post 调整url地址(后台强制转中文)
	 * 
	 * @param {Object} url
	 * 		基于api的相对地址,如: csmconf/conf/confs
	 * @param {Object} param
	 * 		get的数组参数,如:{id:1}
	 * @param {Object} success
	 * 		执行成功的方法 , function(res){}
	 */
	u._adjust_ajaxurl = function(url) {
		let that = this;
		let baseurl = u.getBaseurl();
		let http_url = baseurl + url;

		//clogin由于使用的fa_user的登录因为后台语言更新,会造成后台预览英文版后整个网站都变成英文版,考虑到其他页面不会使用其后台的语言,所以都强制Wie中文版
		// if (!(http_url.indexOf('?lang=') >= 0 || http_url.indexOf('?lang=') >= 0)) {
		// 	let sep = "?";
		// 	if (http_url.indexOf('?') >= 0) {
		// 		sep = "&";
		// 	}
		// 	http_url += sep + "lang=zh-cn";
		// }
		return http_url;
	};

	/**
	 * Ajax Post请求
	 * 
	 * @param {Object} url
	 * 		基于api的相对地址,如: csmconf/conf/confs
	 * @param {Object} param
	 * 		post的数组参数,如:{id:1}
	 * @param {Object} success
	 * 		执行成功的方法 , function(res){}
	 */
	u.ajaxpost = function(url, param, successFunc, errorFunc) {
		let that = this;
		//vm.$u.post(url, param).then(success);
		//let baseurl = u.getBaseurl();
		let http_url = that._adjust_ajaxurl(url);

		uni.$u.http.post(http_url, param).then(res => {
			successFunc(res);
		}).catch(err => {
			if (errorFunc) errorFunc(err);
		})

	};


	/**
	 * <u-swiper :list="sr.conf.images" mode="widthFit"></u-swiper>
	 * that.sr.conf.images = that.$u.c.uswiperAdjustImages('1.jpg,2.jpg');
	 */
	u.uswiperAdjustImages = (images) => {
		let sr = [];
		if (images != null && images != '') {
			let imagearr = images.split(',');
			for (let i = 0, j = imagearr.length; i < j; i++) {
				sr.push({
					"image": imagearr[i],
					"title": ""
				});

			}
		}
		return sr;
	};

	/**
	 * richtext中图片设置为100%宽度
	 * <rich-text class="c-content" :nodes="item.content"></rich-text>
	 * that.sr.item.content = that.$u.c.richtextAdjust(that.sr.item.content);
	 */
	u.richtextAdjust = (html) => {
		let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
			match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
			match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
			match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
			return match;
		});
		newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
			match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
				'max-width:100%;');
			return match;
		});
		newContent = newContent.replace(/<br[^>]*\/>/gi, '');
		newContent = newContent.replace(/\<img/gi,
			'<img style="max-width:100%;height:auto;display:block;margin:10px 0;"');
		return newContent;
	}
	//轻提示的封装
	u.showToast = (title, data, func) => {
		data = data || {};
		uni.showToast({
			title: title,
			icon: data.icon || 'none',
			duration: data.duration || 3000,
			image: data.image || '',
			mask: data.mask || false,
			position: data.position || 'center',
		});
		if (func) {
			setTimeout(func(), 2000);
		}
	};

	u.showModal = (config) => {
		console.log('showModal.config=', config);
		uni.showModal({
			title: (config.title != null && config.title != '') ? config.title : "",
			content: (config.content != null && config.content != '') ? config.content : "",
			cancelText: vm.$t('ccore.btn.cancel'),
			confirmText: vm.$t('ccore.btn.confirm'),
			showCancel:(config.showCancel===false) ? false : true,
			success: (res) => {
				config.success(res);
			}
		})
	};

	vm.$u.c = u;
}

export default {
	install
}
